/**
 * Created by Wangwei on 2017/1/4.
 */
layui.define(['config'], function (exports) {
    var storeKey = 'store_menu',storage = window.localStorage,global = {},$setting=layui.config;
    // 全局变量（数据、函数）
    global.data = {};global.cnf = {};global.fn = {};global.fn.table = {};global.cnf.table = {};
    // 用户信息
    global.fn.getUserData = function() {
        return eval('(' + storage.getItem(storeKey) + ')');
    };
    global.fn.storeUserData = function(data) {
        storage.setItem(storeKey,data);
    };
    global.fn.removeUserData = function() {
        storage.removeItem(storeKey);
    };
    // form表单数据转json
    global.fn.serializeObject = function(obj) {
        var o = {};
        var a = obj.serializeArray();
        $.each(a,function() {
            if (o[this.name]) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value);
            } else if (this.value){// 空值过滤
                o[this.name] = this.value;
            }
        });
        return o;
    };
    global.fn.clear = function(obj) {
        var objId = obj[0];
        //var objId = document.getElementById(id);
        if (objId == undefined) {
            return;
        }
        for (var i = 0; i < objId.elements.length; i++) {
            if (objId.elements[i].type == "text") {
                objId.elements[i].value = "";
            }
            else if (objId.elements[i].type == "password") {
                objId.elements[i].value = "";
            }
            else if (objId.elements[i].type == "radio") {
                objId.elements[i].checked = false;
            }
            else if (objId.elements[i].type == "checkbox") {
                objId.elements[i].checked = false;
            }
            else if (objId.elements[i].type == "select-one") {
                objId.elements[i].options[0].selected = true;
            }
            else if (objId.elements[i].type == "select-multiple") {
                for (var j = 0; j < objId.elements[i].options.length; j++) {
                    objId.elements[i].options[j].selected = false;
                }
            }
            else if (objId.elements[i].type == "textarea") {
                objId.elements[i].value = "";
            }
        }
    };
    global.cnf.table.crossDomain =  {
        xhrFields: {        //跨域
            withCredentials: true
        },
        crossDomain: true
    };
    global.fn.table.queryParams = function(params){
        return {
            pageSize: params.limit, //页面大小
            pageOffset: params.offset //偏移量
        }
    };
    global.fn.table.response = function(data) {
        if (data.code == '10000') {  //未登录
            if(!parent.window.hasAlert) {
                parent.window.hasAlert = true;
                // layer.alert('未登录', {
                //     title: ['提示', 'font-size:15px;text-align:left;padding-left: 10px;'],
                //     skin: 'layui-layer-molv' //样式类名
                //     , closeBtn: 0
                // }, function () {
                    parent.location.href = $setting.getLocalUri() + '/login.html';
                // });
            }
            return false;
        }
        if(data.code!='0') {
            layer.closeAll('loading');
            layer.msg(data.message?data.message: '获取失败',{icon: 2});
        }
        data.total = data.total;
        data.list = data.data?data.data:[];
        return data;
    };
    global.fn.table.onLoadSuccess = function() {
        layer.closeAll('loading');
    };
    global.fn.table.onLoadError = function() {
        layer.closeAll('loading');
    };
    global.fn.replaceLineBreak = function(value) {
        var ret = value;
        if (ret.indexOf('\r\n') > -1) {
            ret = ret.replace(/\r\n/gm, "<br>");
        } else if (ret.indexOf('\r') > -1) {
            ret = ret.replace(/\r/gm, "<br>");
        } else if (ret.indexOf('\n') > -1) {
            ret = ret.replace(/\n/gm, "<br>");
        }
        return ret;
    };
    global.fn.format = function(date, fmt) {
        var o = {
            "M+": date.getMonth() + 1, //月份
            "d+": date.getDate(), //日
            "h+": date.getHours(), //小时
            "m+": date.getMinutes(), //分
            "s+": date.getSeconds(), //秒
            "q+": Math.floor((date.getMonth() + 3) / 3), //季度
            "S": date.getMilliseconds() //毫秒
        };
        if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    };
    /**
     * 判断  大小
     * @param v
     * @param c
     * @returns {boolean}
     */
    global.fn.isMax = function (v, c) {
        return v>c;
    };
    /**
     * 判断 是否在 min,max 区间 (min, max)
     * @param min
     * @param c
     * @param max
     * @returns {boolean}
     */
    global.fn.isBetween = function (min, c, max) {
        return max>c && c>min;
    };

    /**
     * 查询 地址栏参数方法
     * @param name
     * @returns {*}
     */
    global.fn.getQueryString= function (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null)return decodeURI(r[2]);
        return null;
    };
    exports('common', global);
});